package com.jzt.qf.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.jzt.qf.beans.HttpResult;
import com.jzt.qf.beans.UploadBase;
import com.jzt.qf.beans.jztBean.GET_CKFH;
import com.jzt.qf.beans.jztBean.GET_YSJL;
import com.jzt.qf.beans.jztBean.JztYSJLJson;
import com.jzt.qf.beans.qfBean.ZXY_CreateWTYSJL;
import com.jzt.qf.beans.qfBean.ZXY_UpdateCKFH;
import com.jzt.qf.beans.qfBean.ZxyUploadbillJzt;
import com.jzt.qf.beans.uploadJson.JztCKFHjlJson;
import com.jzt.qf.dao.JLDao;
import com.jzt.qf.dao.UploadbillJztDao;
import com.jzt.qf.service.GetService.GetJLService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;


/**
 * 拉取记录定时器
 */
@Slf4j
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class GetRecordsTask {

    @Resource
    private UploadbillJztDao zxyUploadbillDao;
    @Resource
    private GetJLService getJLService;
    @Resource
    private JLDao jlDao;

    @Async
    @Scheduled(fixedRate = 200000)
    //TODO 获取运输记录定时器
    public void GetYSJLTask() throws InterruptedException {
        List<ZxyUploadbillJzt> ysjlList = zxyUploadbillDao.getUntreatedSYJL();
        Set<String> ysjlSet = new TreeSet<>();
        for (ZxyUploadbillJzt zxyUploadbill : ysjlList) {
            ysjlSet.add(zxyUploadbill.getBillcode());
        }
        for (String billCode : ysjlSet) {
            Thread.sleep(1000);
            JztYSJLJson jztYSJLJson = new JztYSJLJson(billCode, LocalDate.now().toString());
            UploadBase<JztYSJLJson> uploadBase = new UploadBase("ysjl", "select", jztYSJLJson);
            HttpResult httpResult = getJLService.getYSJL(uploadBase);
            List<GET_YSJL> ysjls = JSONArray.parseArray(httpResult.getMsgInfo().toString(), GET_YSJL.class);
            for (GET_YSJL ysjl : ysjls) {
                if (!StringUtils.isEmpty(ysjl.getVehicle_No())
                        && !StringUtils.isEmpty(ysjl.getDriver_Staff())
//                        || !ysjl.getDriver_Staff().equals("null")
                ) {
                    log.error("获取运输记录>>>>>:" + JSON.toJSONString(httpResult));
                    ZXY_CreateWTYSJL createWTYSJL = new ZXY_CreateWTYSJL();
                    createWTYSJL.setShengchen_date(ysjl.getCreated_Time().substring(0, 10));
                    createWTYSJL.setFah_diz(ysjl.getCon_Addr_Phone());//发货地址
                    createWTYSJL.setDiz_phone(ysjl.getSsa_Addr_Phone());//送货地址电话
                    createWTYSJL.setBillcode(ysjl.getBusinessbill_No());//单据编号
                    createWTYSJL.setZxs(ysjl.getActual_Pcs() + "");//总箱数
                    createWTYSJL.setYuns_way(ysjl.getTransport_Tool());//运输方式
                    createWTYSJL.setSiji(ysjl.getDriver_Staff()); //司机
                    createWTYSJL.setYuns_unit(ysjl.getCarrier_Name());//运输单位
                    createWTYSJL.setChepai_no(ysjl.getVehicle_No());//车票号
                    createWTYSJL.setDanw_name(ysjl.getSsa_Name());//客户名称
                    createWTYSJL.setQiyun_date(ysjl.getCreated_Time());//起运时间(发货时间)
                    createWTYSJL.setTransportstaffName(ysjl.getTransportstaff_Name());//委托负责人
                    jlDao.createWTYSJL(createWTYSJL);
                } else {
                    log.info(ysjl.getBusinessbill_No() + ">>>未检车到车牌或司机,无法生成运输记录");
                }
            }

        }

    }

    @Async
    @Scheduled(fixedRate = 130000)
    //TODO 获取出库复核定时器
    public void GetCKFHTask() {
        List<ZxyUploadbillJzt> ckfhList = zxyUploadbillDao.getUntreatedCKFH();
        Set<String> ckfhSet = new TreeSet<>();
        for (ZxyUploadbillJzt zxyUploadbill : ckfhList) {
            ckfhSet.add(zxyUploadbill.getBillcode());
        }
        for (String billCode : ckfhSet) {
            JztCKFHjlJson ckfuJLJson = new JztCKFHjlJson(billCode, LocalDate.now().toString(), "");
            UploadBase<JztCKFHjlJson> uploadBase = new UploadBase("xsckjl", "select", ckfuJLJson);
            HttpResult httpResult = getJLService.getCKFH(uploadBase);
            List<GET_CKFH> getCkfhList = JSONArray.parseArray(httpResult.getMsgInfo().toString(), GET_CKFH.class);
            for (GET_CKFH get_ckfh : getCkfhList) {
                if (get_ckfh.getRecheck_Staff().equals("")) {
                    continue;
                }
                ZXY_UpdateCKFH zxy_updateCKFH = new ZXY_UpdateCKFH();
                zxy_updateCKFH.setBillCode(get_ckfh.getBusinessbill_No());
                zxy_updateCKFH.setGoodsNo(get_ckfh.getGoods_No());
                zxy_updateCKFH.setRecheckStaff(get_ckfh.getRecheck_Staff());
                jlDao.updateCKFH(zxy_updateCKFH);
            }
        }

    }


    @Async
    @Scheduled(fixedRate = 130000)
    //TODO 获取九州通报损数据
    public void GetLossTask() {
        List<ZxyUploadbillJzt> lossBills = zxyUploadbillDao.getBackWriteBillByBilltype(9);
        Set<String> lossSet = new TreeSet<>();
        for (ZxyUploadbillJzt jzt_saleBill : lossBills) {
            lossSet.add(jzt_saleBill.getBillcode());
        }
        for (String billCode : lossSet) {
            JztCKFHjlJson ckfuJLJson = new JztCKFHjlJson(billCode, LocalDate.now().toString(), "");
            UploadBase<JztCKFHjlJson> uploadBase = new UploadBase("bsbs", "select", ckfuJLJson);
            HttpResult httpResult = getJLService.getLoss(uploadBase);

        }

    }

}
